/**
 * 
 */
package cl.coretech.openbravo.translator.ws.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.xml.rpc.ServiceException;

import cl.coretech.openbravo.translator.ws.SvnWsLocator;
import cl.coretech.openbravo.translator.ws.SvnWsSEI;


/**
 * @author jsanta
 *
 */
public class SvnWSClient {
	
	private String url            = "";
	@SuppressWarnings("unused")
	private String[] userInfo     = null;
	@SuppressWarnings("unused")
	private byte[] data           = null;
	private Properties properties = null;
	private String target         = "";	
	
	public SvnWSClient(byte[] data, Properties properties, String target){
		this.data       = data;	
		this.properties = properties;	
		this.target     = target;	
		
		String[] userInfo = {
			"OBLanguages",
			target,
			generateComment(properties)
		};
		this.userInfo = userInfo;
		
		this.url = properties.getProperty("url");
	}

	private String generateComment(Properties properties) {
		String user = properties.getProperty("name");
		user        = user + " " + properties.getProperty("lastname");
		String company = properties.getProperty("company");
		String email   = properties.getProperty("email");
		
		String comment = user + "\n" + company + "\n" + email;
		
		return comment;
	}

	public String uploadFile() {
		// TODO Auto-generated method stub
		String response = "ERROR";
		try {
			URL url = new URL(this.url);
			SvnWsSEI svnWsSEI = new SvnWsLocator().getSvnWsSEIPort(url);
			
			response = svnWsSEI.commitFile(data, userInfo);
			
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return response;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the userInfo
	 */
	public String[] getUserInfo() {
		return userInfo;
	}

	/**
	 * @param userInfo the userInfo to set
	 */
	public void setUserInfo(String[] userInfo) {
		this.userInfo = userInfo;
	}

	/**
	 * @return the data
	 */
	public byte[] getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(byte[] data) {
		this.data = data;
	}

	/**
	 * @return the properties
	 */
	public Properties getProperties() {
		return properties;
	}

	/**
	 * @param properties the properties to set
	 */
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	/**
	 * @return the target
	 */
	public String getTarget() {
		return target;
	}

	/**
	 * @param target the target to set
	 */
	public void setTarget(String target) {
		this.target = target;
	}

}
